# -*- coding: utf-8 -*-

import re
import unicodedata
from datetime import datetime, timedelta

from config import *

ps = rangetable.values() + [u"^"+u"".join(rangetable.values() + list(igns))]

glob_true = u"|".join(u"[%s]+[*?]*" % x  for x in ps)
glob_false = u"|".join(u"[%s]+" % x  for x in ps)

# pattern = re.compile(u"[a-zA-Z0-9_]+|[\uac00-\ud7af]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u30ff]+", re.UNICODE)
# pattern_g = re.compile(u"[a-zA-Z0-9_]+[*?]*|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u30ff\uac00-\ud7af]+[*?]*", re.UNICODE)
pattern = re.compile(glob_false, re.UNICODE)
pattern_g = re.compile(glob_true, re.UNICODE)

KANA_MIN = ord(u"ア")
KANA_MAX = ord(u"ア") + 85
KANA_DIF = ord(u"あ") - ord(u"ア")
# KIGOS = frozenset(ord(x) for x in u"-ー")

def bigrams(xs, isglob=False):
    for x in xs:
        for y in bigrams_(x, isglob):
            yield y

def bigrams_(s, isglob):
    assert isinstance(s, unicode)
    # normed = unicodedata.normalize('NFKC', s)
    ## return bisplitx((pattern_g if isglob else pattern).findall(normed) , isglob)

    # return bisplitx((isglob and pattern_g or pattern).findall(normed) , isglob)
    if isglob:
        return (isglob and pattern_g or pattern).findall(s)
    return bisplitx((isglob and pattern_g or pattern).findall(s) , isglob)



def bisplitx(xss, isglob):
    for xs in xss:
        if not xs:
            break
        if ord(xs[0]) <= 122:
            yield xs
        else:
            for x in bisplit(xs, isglob):
                # yield kananormalize(x)
                yield x
            ## for x in bisplit(kananormalize(xs), isglob):
            ##     yield x

def bisplit(xs, isglob):
    assert xs
    length = len(xs)
    if length == 1 and isglob:
        yield xs + "*"
        return
    ## for i in xrange(length - 1 if isglob else length):
    last = (not isglob and length != 2) and length or length - 1
    for i in xrange(last):
        w = xs[i:i+2]
        yield w

def kananormalize(xs):
    assert isinstance(xs, unicode)
    return u"".join(
        ## unichr(ord(x) + KANA_DIF) if KANA_MIN <= ord(x) <= KANA_MAX else x
        (KANA_MIN <= ord(x) <= KANA_MAX) and unichr(ord(x) + KANA_DIF) or x
        for x in xs)



def test_comp_cjk():
   words = [
#       "abc def今日は",
#       "言語はpythonです",
#       "スペース を",
#       "記号*のテスト",
#       "記号#のテスト",
#       "###$$$%%%&&&記号は無視のaaaようです",
       u"マクロ範囲",
       u"いまだ未定",
       u"まだ未定",
       u"未定",
       u"ランチにぎり",
       u"abc def.hello!@;world",
       u"avàc123",
       u"avàcあいう穂",
       u"穂",
       u"t?es***t",
       u"け",
       u"a",
       u"テスト",
       u"ﾃｽﾄ",
       u"食べた",
       u"会",
       u"会社",
       u"会社案",
       u"会社案内",       
       u"국립국어원",
       u"あい어원",
       u"ฉันขาหัก",
       ]
   for word in words:
       print '=====now test:', word
       u = word
       print 'no glob result:'
       for i in bigrams_(u,False):
           print i
#           print i.encode('gbk')

       print 'glob result:'
       
       for i in bigrams([u], True):
           print i

if __name__=="__main__":
    test_comp_cjk()
    assert u"あいうえおあいうえお" == kananormalize(u"アイウエオあいうえお")
